﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YJKStairs.CommonUnit
{
	public class FilterElementCls
	{
		public static void GetNeedDealEle(Autodesk.Revit.DB.Document docRoot, List<Type> ListSelElemType, List<RevitLinkInstance> LinkParents, ref Dictionary<Type, List<LinkElement>> DicNeedDealLinkEles)
		{
			Document document = null;
			if (LinkParents.Count == 0)
			{
				document = docRoot;
			}
			else
			{
				document = LinkParents[LinkParents.Count - 1].GetLinkDocument();
			}
			if (document == null)
			{
				return;
			}
			foreach (Type type in ListSelElemType)
			{
				ElementClassFilter elementClassFilter = new ElementClassFilter(type);
				IEnumerable<Element> enumerable = new FilteredElementCollector(document).WherePasses(elementClassFilter).ToList<Element>();
				List<LinkElement> list = new List<LinkElement>();
				foreach (Element element in enumerable)
				{
					LinkElement item = new LinkElement(docRoot, LinkParents, element.Id);
					list.Add(item);
				}
				if (list.Count > 0)
				{
					if (DicNeedDealLinkEles.ContainsKey(type))
					{
						DicNeedDealLinkEles[type].AddRange(list);
					}
					else
					{
						DicNeedDealLinkEles.Add(type, list);
					}
				}
			}
			List<RevitLinkInstance> list2 = new List<RevitLinkInstance>();
			if (!FilterElementCls.GetLinkDocument(ref list2, document))
			{
				return;
			}
			for (int i = 0; i < list2.Count; i++)
			{
				if (list2[i] != null)
				{
					List<RevitLinkInstance> list3 = new List<RevitLinkInstance>();
					list3.AddRange(LinkParents);
					list3.Add(list2[i]);
					FilterElementCls.GetNeedDealEle(docRoot, ListSelElemType, list3, ref DicNeedDealLinkEles);
				}
			}
		}

		public static bool GetLinkDocument(ref List<RevitLinkInstance> inss, Autodesk.Revit.DB.Document document)
		{
			if (document == null)
			{
				return false;
			}
			new List<Document>();
			inss = document.GetElements<RevitLinkInstance>().ToList<RevitLinkInstance>();
			return inss.Count != 0;
		}

		public static void GetNeedDealEle(Autodesk.Revit.DB.Document docRoot, List<BuiltInCategory> lstSelCategory, List<RevitLinkInstance> LinkParents, ref Dictionary<BuiltInCategory, List<LinkElement>> DicNeedDealLinkEles)
		{
			Document document = null;
			if (LinkParents.Count == 0)
			{
				document = docRoot;
			}
			else
			{
				document = LinkParents[LinkParents.Count - 1].GetLinkDocument();
			}
			if (document == null)
			{
				return;
			}
			foreach (BuiltInCategory builtInCategory in lstSelCategory)
			{
				ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(builtInCategory);
				IEnumerable<Element> enumerable = new FilteredElementCollector(document).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToList<Element>();
				List<LinkElement> list = new List<LinkElement>();
				foreach (Element element in enumerable)
				{
					LinkElement item = new LinkElement(docRoot, LinkParents, element.Id);
					list.Add(item);
				}
				if (list.Count > 0)
				{
					if (DicNeedDealLinkEles.ContainsKey(builtInCategory))
					{
						DicNeedDealLinkEles[builtInCategory].AddRange(list);
					}
					else
					{
						DicNeedDealLinkEles.Add(builtInCategory, list);
					}
				}
			}
			List<RevitLinkInstance> list2 = new List<RevitLinkInstance>();
			if (!FilterElementCls.GetLinkDocument(ref list2, document))
			{
				return;
			}
			for (int i = 0; i < list2.Count; i++)
			{
				if (list2[i] != null)
				{
					List<RevitLinkInstance> list3 = new List<RevitLinkInstance>();
					list3.AddRange(LinkParents);
					list3.Add(list2[i]);
					FilterElementCls.GetNeedDealEle(docRoot, lstSelCategory, list3, ref DicNeedDealLinkEles);
				}
			}
		}
	}
}
